package study.source.sunghun.controller;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.View;

import study.source.sunghun.service.email.MailService;

@Controller
@RequestMapping("/mail")
public class MailController {
	private static Logger logger = LoggerFactory.getLogger(MailController.class);
	
	@Autowired 
	private MailService service;
	
	@Autowired
	private View jsonView;
	
	/**
	 * Send Mail
	 * 
	 * @param request
	 * @param modelMap
	 * @param from
	 * @param to
	 * @param subject
	 * @param msg
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/send", method = RequestMethod.POST)
	public View sendMail(HttpServletRequest request, ModelMap modelMap,
			@RequestParam String from, @RequestParam String to,
			@RequestParam String subject, @RequestParam String msg) throws Exception {
		logger.debug("========= Send Mail ==========");
		
		logger.info("from = " + from);
		logger.info("to = " + to);
		logger.info("subject = " + subject);
		logger.info("msg = " + msg);
		
		service.sendMail(from, to, subject, msg);		
		
		modelMap.addAttribute("result", "true");
		return jsonView;
	}
	
	
}






